<%@ page import="BusinessLogic.ControllerFacade" %>
<%@ page import="Presentation.WebUI.LinksGenerator" %>
<%@ page import="Domain.DataStructures.IUserData" %>
<%@ page errorPage="error.jsp" %>

<%
    ControllerFacade c = (ControllerFacade) session.getAttribute("c");
    IUserData user = c.getCurrentUser();
    String username = user.getUsername();
%>

<h2>Search movies</h2>

<%
    String keyword = request.getParameter("keyword");
    String method = request.getParameter("method");
    int amount = c.getListLength();
    int start = 0;

    String startParam = request.getParameter("start");
    String addRec = request.getParameter("addRec");
    String showGraph = request.getParameter("showGraph");
    String userRated = request.getParameter("userRated");
    String startWith = request.getParameter("startWith");
	
    if (startParam != null)
        start = Integer.parseInt(startParam);

    String[] list = c.searchMovie(keyword,method,start,startWith);

    String startMessage = "";
    if (startWith != null)
        startMessage = " starts with "+startWith;

    if(method.equals("ALL"))
        out.println("<h3>Showing ALL movies"+startMessage+"</h3>");
    else
        out.println("<h3>Showing movies where "+method+" is "+keyword.toUpperCase()+" "+startMessage+"</h3>");

    out.println(LinksGenerator.letterNav(request.getParameterMap()));


    String[] show = new String[list.length];
    int i = 0;
    for(String m : list) {
        String s = LinksGenerator.movieLink(m);
        if (addRec != null && c.getUnrecomendedUserMovies().contains(m)) {
            s += "&nbsp;";
            s += LinksGenerator.recommendationLink(m);
        }
        if (showGraph != null && (user.isManager() || user.isAdministrator())) {
            s += "&nbsp;";
            s += LinksGenerator.showGraphLink(m);
        }
        if (userRated != null && (user.isManager() || user.isAdministrator())) {
            s = LinksGenerator.showUserRated(m,s+"&nbsp;");
        }
        show[i] = s;
        i++;
    }

    String last = "<br />No More Movies<br /><br />";
    out.println(LinksGenerator.showListWithNavButtons(show, request.getParameterMap(), start, amount, last));
%>
